package com.hsj.common.system.controller;

import com.cjxch.supermybatis.base.bean.Pager;
import com.hsj.common.system.convert.MenuPrivilegeConvert;
import com.hsj.common.system.entity.MenuPrivilegeDo;
import com.hsj.common.system.service.IMenuPrivilegeService;
import com.hsj.common.system.vo.request.ReqMenuPrivilegeCreateVo;
import com.hsj.common.system.vo.request.ReqMenuPrivilegeUpdateVo;
import com.hsj.common.system.vo.request.ReqMenuPrivilegePageQueryVo;
import com.hsj.common.system.vo.response.RespMenuPrivilegeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;


/**
 * 菜单接口权限 Controller
 * @Author: Czy
 * @Date: 2024-02-03 16:08:04
 * @Email: 314170122@qq.com
 */
@Api(tags = "菜单接口权限管理")
@RestController
@RequestMapping("/menuPrivilege")
@Slf4j
public class MenuPrivilegeController {

    private final IMenuPrivilegeService menuPrivilegeService;
    private final MenuPrivilegeConvert menuPrivilegeConvert;

    public MenuPrivilegeController(IMenuPrivilegeService menuPrivilegeService, MenuPrivilegeConvert menuPrivilegeConvert) {
        this.menuPrivilegeService = menuPrivilegeService;
        this.menuPrivilegeConvert = menuPrivilegeConvert;
    }

    @PostMapping("createMenuPrivilege")
    @ApiOperation(value = "创建菜单接口权限", notes = "创建菜单接口权限", httpMethod = "POST")
    public Serializable createMenuPrivilege(@Valid @RequestBody ReqMenuPrivilegeCreateVo vo){
        return menuPrivilegeService.insert(menuPrivilegeConvert.voToDo(vo));
    }

    @PostMapping("updateMenuPrivilege")
    @ApiOperation("修改菜单接口权限")
    public Serializable updateMenuPrivilege(@Valid @RequestBody ReqMenuPrivilegeUpdateVo vo){
        return menuPrivilegeService.update(menuPrivilegeConvert.voToDo(vo));
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除菜单接口权限")
    public Boolean delete(@PathVariable("id")String id){
        return menuPrivilegeService.delete(id) == 1L;
    }

    @PostMapping("/pageList")
    @ApiOperation("分页查询菜单接口权限")
    public Pager<RespMenuPrivilegeVo> pageList(@Valid @RequestBody ReqMenuPrivilegePageQueryVo vo){
        Pager<MenuPrivilegeDo> pager = new Pager<>();
        pager.setPageNumber(vo.getPageNumber());
        pager.setPageSize(vo.getPageSize());
        pager = menuPrivilegeService.getPager(pager, vo.getSmCriteria());
        pager.setResult(menuPrivilegeConvert.doToVo(pager.getResult()));
        Pager<RespMenuPrivilegeVo> retPager = new Pager<>();
        BeanUtils.copyProperties(pager, retPager);
        return retPager;
    }

    @GetMapping("/get/{id}")
    @ApiOperation("获取菜单接口权限信息")
    public RespMenuPrivilegeVo get(@PathVariable("id")String id) {
        return menuPrivilegeConvert.doToVo(menuPrivilegeService.get(id));
    }
}
